package com.ow.module.event.sysmgr;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ow.framework.common.Constant;
import com.ow.framework.data.po.ResultData;
import com.ow.framework.exception.DetailException;
import com.ow.framework.listener.event.EventExecutor;
import com.ow.framework.listener.event.EventListener;
import com.ow.framework.util.Encrypt;




public class SuperPasswordModifyEvent implements EventListener{

	
	/**异常处理*/
	static Logger log = Logger.getLogger(Constant.FRAMEWORK);
	private ResultData resultData = new ResultData();
	
	public ResultData event(EventExecutor eventExecutor) throws Exception {

		try
		{ 
			String newPassword = eventExecutor.getBaseAction().getParamMap().get("newPassword");
			String userID = eventExecutor.getBaseAction().getParamMap().get("userID");
			Map mapForNew = new HashMap();
			mapForNew.put("userID", userID);
			/**转换密码为md5格式*/
			if (!"".equals(newPassword))
				newPassword = Encrypt.getMD5(newPassword.trim());
			mapForNew.put("newPassword", newPassword);
			/**验证原密码*/
			String s_userID = eventExecutor.getBaseAction().session.get("s_userID").toString();
			Map map = new HashMap();
			map.put("s_userID", s_userID);
			Object one = eventExecutor.getSqlSession().selectOne("user.get_password_by_id", map);
			if(one != null){
				/**设置新密码*/
				String sid = eventExecutor.getSqlidByIndex(0);
				if (!"".equals(sid))
				{
					int rvalue = eventExecutor.getSqlSession().update(sid, mapForNew);
					resultData.setIntResult(rvalue);
				}
				resultData.setIntResult(1);
				return resultData;
				
			}else{
				eventExecutor.getBaseAction().getPromptInfo().setResultDescInfo(eventExecutor.getBaseAction().getText("not_login"));
				resultData.setIntResult(0); 
				throw new Exception ("user not login");
			}
		}
		catch (Exception e)
		{
			/**执行器执行失败*/
			String error = DetailException.expDetail(e, PasswordModifyEvent.class);
			log.error(error);
			throw e;
		}
	}

}
